<?php
class MembershipModel extends Model {
	private $table1 ='membership';
	private $table2 = 'collect';
	private $table3 = 'products';
	private $table4 = 'categories_description';
	private $table5 = 'products_to_categories';	
	private $table6 = 'browsing_record';
	private $table7 = 'member_moneylog';
	private $table8 = 'member_withdraw';
	
	//视图表
	private $table9 = 'membercount';
	private $table10 = 'auction';
	
	/**
	 * 根据条件更新会员信息
	 * $data 更新信息 $where
	 */
	public function registerinfo($data,$where){
			$sql="UPDATE ".$this->table1." SET member_id='".$data['member_id']."', name='".$data['name']."', tel='".$data['tel'].
			"', join_date='".$data['join_date']."',pwd='".$data['pwd']."' WHERE ".$where;
		   $this->query($sql);
	}
	
	/**
	 * 获取会员收藏信息
	 * @param $uid $order $limit
	 */
	public function getCollect($uid,$order,$limit){
		//M($this->table3)->table($this->table3." c")->join($this->table2." p ON c.products_id=p.products_id")->where($where)->order($order)->limit($limit);
		//echo M($this->table2)->getLastSql();die;
		$sql = "SELECT p.products_id,p.products_image,p.products_name,p.products_date_added FROM ".$this->table3." p LEFT JOIN ".$this->table2." co ON p.products_id=co.products_id WHERE co.uid=".$uid." ORDER BY ".$order." LIMIT ".$limit;
		//echo $sql;die;
		$result = M($this->table3)->query($sql);
		foreach($result as $key=>$vo){
			$sql = "SELECT cd.categories_name, cd.categories_id FROM ".$this->table4." cd LEFT JOIN ".$this->table5." ptc ON cd.categories_id=ptc.categories_id WHERE ptc.products_id=".$vo['products_id'];
			$cate = M($this->table4)->query($sql);
			//var_dump($cate[0]);die;
			$result[$key]['cate']=$cate[0];
		}
		return $result;
	}
	
	/**
	 * 获取会员浏览记录
	 * @param $uid $order $limit
	 */
	public function getBrowsing($uid,$order,$limit){
		$sql = "SELECT DISTINCT(p.products_id),p.products_image,p.products_name,p.products_date_added,p.viewd,p.products_type,p.products_price FROM ".$this->table3." p LEFT JOIN ".$this->table6." co ON p.products_id=co.products_id WHERE co.uid=".$uid." ORDER BY ".$order." LIMIT ".$limit;
		//echo $sql;die;
		$result = M($this->table3)->query($sql);
		foreach($result as $key=>$vo){
			$sql = "SELECT cd.categories_name, cd.categories_id FROM ".$this->table4." cd LEFT JOIN ".$this->table5." ptc ON cd.categories_id=ptc.categories_id WHERE ptc.products_id=".$vo['products_id'];
			$cate = M($this->table4)->query($sql);
			//var_dump($cate[0]);die;
			$result[$key]['cate']=$cate[0];
			
			if($vo['products_type']==2){
				$sql = "SELECT price FROM ".$this->table10." WHERE products_id=".$vo['products_id']." ORDER BY auction_id desc limit 1";
				
				$auction = M($this->table10)->query($sql);//where(array('products_id'=>$vo['products_id']))->field('price')->order('auction_id desc')->limit(1);
				
				$result[$key]['auction_price']=$auction['price']?$auction['price']:$vo['products_price'];
			}
		}
		
		return $result;
	}
	
	/**
	 * 获取会员账户余额及名称
	 * @param $where
	 */
	public function getAccountMoney($where,$field){
		return M($this->table7)->table($this->table1." m")->join($this->table7." mm ON m.uid=mm.uid")->where()->field($field)->order('mm.member_moneylog_id desc')->find();
	}
	
	/**
	 * 按照天数获取会员个数
	 * @param string $where
	 */
	public function getMcByday($where=1,$limit,$order){
		if($limit){
			$sql = "select DATE_FORMAT(join_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(join_date,'%Y%m%d') ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select DATE_FORMAT(join_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(join_date,'%Y%m%d') ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}
	
	
	/**
	 * 获取按天数获取产品的个数
	 * @param string  $where
	 */
	public function getMc($where=1){
		$sql = "select DATE_FORMAT(join_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(join_date,'%Y%m%d') ";
		$result = M($this->table1)->query($sql);
		return count($result);
	}
	/**
	 * 根据日期获取会员详情
	 * @param string $where
	 */
	public function getMcDetail($where=1,$limit,$order){
		if($limit){
			$sql = "select uid, member_id,name, tel from ".$this->table1." WHERE ".$where." ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select uid, member_id,name, tel from ".$this->table1." WHERE ".$where." ORDER BY ".$order;
		}
		//echo $sql;die;
		$result =  M($this->table1)->query($sql);
		foreach($result as $key=>$val){
			//获取会员金额
			$account = M('member_moneylog')->where(array('uid'=>$val['uid']))->order('member_moneylog_id desc')->field('active_money,freeze_money')->find();
			if($account)$account['all_account']=$account['active_money']+$account['freeze_money'];
			$result[$key]['account_money'] = $account?$account:0;
				
			//获取会员交易评分
			$point = D('Appraise')->getAverage($val['uid']);
			//$allpoint = M('appraise')->where(array('uid'=>$val['uid']))->sum('point');
			//echo $val['uid']."dd".$point."<br/>";
			$result[$key]['point']=$point;				
		}
		return $result;
	}
	
}